cmake_minimum_required(VERSION 3.22)

# 设置项目名称
project(demo_cos)

# 设置编译器标志
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pg")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations -g ")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-recover -static-libasan")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize-recover -static-libasan")

#set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "-m32")
#set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-m32")

# compile_commands.json文件输出
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 定义可执行文件名称
set(TARGET_NAME demo_cos)

# 创建可执行文件
add_executable(${TARGET_NAME})

# 添加子目录
add_subdirectory(1_cos)
add_subdirectory(2_driver)
add_subdirectory(3_app)

# 为目标可执行文件设置包含目录
target_include_directories(${TARGET_NAME} PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# 链接lds文件
target_link_options(${TARGET_NAME} PUBLIC -T${CMAKE_CURRENT_SOURCE_DIR}/demo_cos.lds)

# 链接 app 模块
target_link_libraries(${TARGET_NAME} PRIVATE -Wl,--whole-archive app -Wl,--no-whole-archive)

# 链接库（如果需要的话）
target_link_libraries(${TARGET_NAME} PRIVATE crypto ssl)

# 查找 Doxygen
find_package(Doxygen REQUIRED)

if(DOXYGEN_FOUND)
    set(DOXYGEN_INPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
    set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/doc)
    # 确保输出目录存在
    file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR})

    # 指定 Doxygen 配置文件
    set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxygen/Doxyfile)
    set(DOXYGEN_OUT ${CMAKE_CURRENT_SOURCE_DIR}/Doxygen/Doxyfile)

    # 配置 Doxygen
    configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)

    # 添加 Doxygen 目标
    add_custom_target(doc
        COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
        WORKING_DIRECTORY ${DOXYGEN_OUTPUT_DIR}
        COMMENT "Generating API documentation with Doxygen"
        VERBATIM
        #DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Doxygen/custom_header.tex
    )

    # 添加生成 PDF 的目标
    add_custom_target(pdf
        COMMAND mkdir -p ${DOXYGEN_OUTPUT_DIR}/latex
        COMMAND ${CMAKE_COMMAND} -E chdir ${DOXYGEN_OUTPUT_DIR}/latex make pdf
        DEPENDS doc
        COMMENT "Generating PDF documentation"
        VERBATIM
    )
endif()
